-
-
Notifications
You must be signed in to change notification settings - Fork 142
feat(validator)!: add localization support for validation error messages #1444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@innocenzi can you create an issue so I can get started? Would help a lot if you have before and after code snippets |
|
@Treggats I opened #1445, but I don't have the definitive list of breaking changes, I'm waiting for Brent's input regarding the rule class names. For now, the main breaking change is that |
|
Ah, I opened a Discord thread before seeing this one :) Let's keep brainstorming in Discord and log our decisions in #1445 |
|
I'm thinking the migration CLI can be done after this PR is merged? Should we target |
|
@innocenzi we should target 2.x with this one |
|
@brendt I went ahead and updated the naming convention to use the predicate format, since reserved keywords are an issue otherwise. Let me know what you think @Treggats here is a rector rule if it helps: ->withConfiguredRule(RenameClassRector::class, [
'Tempest\Validation\Rules\AlphaNumeric' => 'Tempest\Validation\Rules\IsAlphaNumeric',
'Tempest\Validation\Rules\ArrayList' => 'Tempest\Validation\Rules\IsArrayList',
'Tempest\Validation\Rules\BeforeDate' => 'Tempest\Validation\Rules\IsBeforeDate',
'Tempest\Validation\Rules\Between' => 'Tempest\Validation\Rules\IsBetween',
'Tempest\Validation\Rules\BetweenDates' => 'Tempest\Validation\Rules\IsBetweenDates',
'Tempest\Validation\Rules\Count' => 'Tempest\Validation\Rules\HasCount',
'Tempest\Validation\Rules\DateTimeFormat' => 'Tempest\Validation\Rules\HasDateTimeFormat',
'Tempest\Validation\Rules\DivisibleBy' => 'Tempest\Validation\Rules\IsDivisibleBy',
'Tempest\Validation\Rules\Email' => 'Tempest\Validation\Rules\IsEmail',
'Tempest\Validation\Rules\EndsWith' => 'Tempest\Validation\Rules\EndsWith',
'Tempest\Validation\Rules\Even' => 'Tempest\Validation\Rules\IsEvenNumber',
'Tempest\Validation\Rules\HexColor' => 'Tempest\Validation\Rules\IsHexColor',
'Tempest\Validation\Rules\IP' => 'Tempest\Validation\Rules\IsIP',
'Tempest\Validation\Rules\IPv4' => 'Tempest\Validation\Rules\IsIPv4',
'Tempest\Validation\Rules\IPv6' => 'Tempest\Validation\Rules\IsIPv6',
'Tempest\Validation\Rules\In' => 'Tempest\Validation\Rules\IsIn',
'Tempest\Validation\Rules\Json' => 'Tempest\Validation\Rules\IsJsonString',
'Tempest\Validation\Rules\Length' => 'Tempest\Validation\Rules\HasLength',
'Tempest\Validation\Rules\Lowercase' => 'Tempest\Validation\Rules\IsLowercase',
'Tempest\Validation\Rules\MACAddress' => 'Tempest\Validation\Rules\IsMacAddress',
'Tempest\Validation\Rules\MultipleOf' => 'Tempest\Validation\Rules\IsMultipleOf',
'Tempest\Validation\Rules\NotEmpty' => 'Tempest\Validation\Rules\IsNotEmptyString',
'Tempest\Validation\Rules\NotIn' => 'Tempest\Validation\Rules\IsNotIn',
'Tempest\Validation\Rules\NotNull' => 'Tempest\Validation\Rules\IsNotNull',
'Tempest\Validation\Rules\Numeric' => 'Tempest\Validation\Rules\IsNumeric',
'Tempest\Validation\Rules\Odd' => 'Tempest\Validation\Rules\IsOddNumber',
'Tempest\Validation\Rules\Password' => 'Tempest\Validation\Rules\IsPassword',
'Tempest\Validation\Rules\PhoneNumber' => 'Tempest\Validation\Rules\IsPhoneNumber',
'Tempest\Validation\Rules\RegEx' => 'Tempest\Validation\Rules\MatchesRegEx',
'Tempest\Validation\Rules\Time' => 'Tempest\Validation\Rules\IsTime',
'Tempest\Validation\Rules\Timestamp' => 'Tempest\Validation\Rules\IsUnixTimestamp',
'Tempest\Validation\Rules\Timezone' => 'Tempest\Validation\Rules\IsTimezone',
'Tempest\Validation\Rules\Ulid' => 'Tempest\Validation\Rules\IsUlid',
'Tempest\Validation\Rules\Uppercase' => 'Tempest\Validation\Rules\IsUppercase',
'Tempest\Validation\Rules\Url' => 'Tempest\Validation\Rules\IsUrl',
'Tempest\Validation\Rules\Uuid' => 'Tempest\Validation\Rules\IsUuid',
]) |
39dd31a to
e8f6e05
Compare
|
@innocenzi I can use this to create a rule to rename these instances in a codebase :)
|
039c38f to
f9c49e6
Compare
e8f6e05 to
823aae9
Compare
823aae9 to
8ef096a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a solid PR, I rebased it on 2.x, feel free to double-check and merge when it's green
8ef096a to
bf4b687
Compare
bf4b687 to
e679026
Compare
Closes #1389, #266
This pull request updates the validator to make validation error messages provide more context and be localizable.
A few APIs change in this pull request, so it is breaking. The first one is that the
Validatornow needs aTranslatorinstance. The recommended way of working with the validator is now to use dependency injection, instead of manually instantiating it.The
Ruleclass no longer requires amessage()method. Instead, rule classes that also implement theHasErrorMessagemay have a hardcoded validation error message, and more importantly, rule classes that implementHasTranslationVariablesmust return translation variables used in translation files.Tempest now provides a
localization.en.ymltranslation file with default translation messages. This builds on the recently-introduced localization feature, using MessageFormat as the translation message syntax. For instance, validation error message forAfterDatelooks as follows:This expects a
inclusiveanddatetranslation variable, whichAfterDateprovides through itsHasTranslationVariablesimplementation.To get the actual error message, the
Validatorclass now has agetErrorMessagemethod, which accepts aRuleinstance and an optional field name.This pull request also has some minor fixes regarding our intl message formatter, it should be now more compliant and I added support for the
:booleanfunction, which includes a boolean selector.